﻿using System;
using System.Collections.Generic;
using System.Linq;
using CRM.Database.Model;

namespace CRM.Syncroniser
{
  public class LegalEntitiesSynchroniser:ISyncronise
    {
      #region Implementation of ISyncronise

      /// <summary>
      /// Запуск синхронизации
      /// </summary>
      /// <returns>Результат запуска</returns>
      public bool Syncronise()
      {
          var col = new CustomersCollection();
          var leCol = new LegalEntitiesCollection();
          var addrCol = new AddressesCollection();
          var contactCol = new ContactsCollection();
          var turnoverCol = new TurnoversCollection();
          var trainingCol = new TrainingsRequestsCollection();
          var agrCol = new AgreementElementsCollection();

          var leToDel = new List<int>();
          
          foreach(var le in leCol.Entities.LegalEntities.OrderBy(c=>c.IdLegalEntity).ToList())
          {
              if(!leToDel.Contains(le.IdLegalEntity))
              {
                  var lle =
                  leCol.Entities.LegalEntities.FirstOrDefault(
                      c => c.IdLegalEntity != le.IdLegalEntity & c.IdCustomer == le.IdCustomer & c.Name == le.Name);

                  if(lle!=null)
                      if(!leToDel.Contains(lle.IdLegalEntity))
                /****************/

                  {
                      if(lle.NumberInSAP!=null)
                          if(lle.NumberInSAP>0)
                          le.NumberInSAP = lle.NumberInSAP;


                      foreach(var trRequest in leCol.Entities.TrainingsRequests.Where(c=>c.IdLegalEntity==lle.IdLegalEntity))
                      {
                          TrainingsRequests item = trainingCol.GetItemById(trRequest.IdTrainingsRequest) as TrainingsRequests;

                          if (item.IdLegalEntity != le.IdLegalEntity)
                          {
                              item.IdLegalEntity = le.IdLegalEntity;
                              trainingCol.Update(item, "Max"+ DateTime.Now.ToShortDateString() , false);
                          }
                      }

                      trainingCol.Save();

                      foreach(var cont in leCol.Entities.Contacts.Where(c=>c.IdLegalEntity==lle.IdLegalEntity))
                      {
                          Contacts item = contactCol.GetItemById(cont.IdContact) as Contacts;

                          if(item.IdLegalEntity!=le.IdLegalEntity)
                          {
                              item.IdLegalEntity = le.IdLegalEntity;
                              contactCol.Update(item, "Max" + DateTime.Now.ToShortDateString(), false);
                          }

                      }

                      contactCol.Save();

                      foreach (var addr in leCol.Entities.Addresses.Where(c => c.IdLegalEntity == lle.IdLegalEntity))
                      {
                          var item = addrCol.GetItemById(addr.GetKeyValue()) as Addresses;

                          if (item.IdLegalEntity != le.IdLegalEntity)
                          {
                              item.IdLegalEntity = le.IdLegalEntity;
                              addrCol.Update(item, "Max" + DateTime.Now.ToShortDateString(), false);
                          }

                      }

                      addrCol.Save();



                      foreach (var agr in agrCol.Entities.AgreementElements.Where(c => c.IdLegalEntity == lle.IdLegalEntity))
                      {
                          var item = agrCol.GetItemById(agr.GetKeyValue()) as AgreementElements;

                          if (item.IdLegalEntity != le.IdLegalEntity)
                          {
                              item.IdLegalEntity = le.IdLegalEntity;
                              agrCol.Update(item, "Max" + DateTime.Now.ToShortDateString(), false);
                          }

                      }

                      agrCol.Save();

                      foreach (var turn in leCol.Entities.Turnovers.Where(c => c.IdLegalEntity == lle.IdLegalEntity))
                      {
                          var item = turnoverCol.GetItemById(turn.GetKeyValue()) as Turnovers;

                          if (item.IdLegalEntity != le.IdLegalEntity)
                          {
                              item.IdLegalEntity = le.IdLegalEntity;
                              turnoverCol.Update(item, "Max" + DateTime.Now.ToShortDateString(), false);
                          }

                      }

                     



                      foreach (var turn in leCol.Entities.Turnovers.Where(c => c.IdDealerLegalEntity == lle.IdLegalEntity))
                      {
                          var item = turnoverCol.GetItemById(turn.GetKeyValue()) as Turnovers;

                          if (item.IdDealerLegalEntity != le.IdLegalEntity)
                          {
                              item.IdDealerLegalEntity = le.IdLegalEntity;
                              turnoverCol.Update(item, "Max" + DateTime.Now.ToShortDateString(), false);
                          }

                      }

                      turnoverCol.Save();

                     
                      leToDel.Add(lle.IdLegalEntity);
                  }
              }
          }

          leCol.Save();


          var leToDelCol = new LegalEntitiesCollection();

          foreach(var id in leToDel)
          {
              leToDelCol.DeleteById(id);
          }
        
         

          return true;
      }

      

      #endregion


      protected void SetPrimaryAddresses()
      {
          
      }
    }
}
